package com.jajahome.feature;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.jajahome.R;
import com.jajahome.base.BaseActivity;
import com.jajahome.constant.Constant;
import com.jajahome.feature.set.SetDetailAct;
import com.jajahome.pop.PopShare;
import com.jajahome.widget.X5WebView;
import com.sina.weibo.sdk.api.share.IWeiboShareAPI;
import com.sina.weibo.sdk.api.share.WeiboShareSDK;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.io.File;

import butterknife.BindView;

/**
 * Created by ${Terry} on 2016/11/29.
 */
public class TencentWebV extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.tbsContent)
    X5WebView tbsContent;
    @BindView(R.id.set_vr_exit)
    ImageView mBack;
    @BindView(R.id.set_vr_share)
    ImageView vrShareImg;
    @BindView(R.id.isok)
    TextView isok;
    @BindView(R.id.vrWebview)
    android.webkit.WebView mWebView;

    private PopShare mPopShare; //分享弹窗
    private IWXAPI mIwxapi; //微信分享api
    private IWeiboShareAPI mIWeiboShareAPI;      //微博api


    public static final String H5_URL = "H5_URL";
    public static String vrUrl;
    public static String Tag = "TencentWebV";
    public static String DialogUrl;

    @Override
    protected int getViewId() {
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        return R.layout.layout_tencent_web;

    }

    @Override
    protected void initEvent() {
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        mIwxapi = WXAPIFactory.createWXAPI(mContext, Constant.WEICHAT_APP_ID, true);
        //新浪微博分享
        mIWeiboShareAPI = WeiboShareSDK.createWeiboAPI(this, Constant.WEIBO_APP_KEY);
        mIWeiboShareAPI.registerApp();    // 将应用注册到微博客户端
        initHardwareAccelerate();
        DialogUrl = SetDetailAct.mSetUrl;
        if (vrUrl != null) {
           if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
               initWeb();
           }else {
               initX5();
           }
    }
        setListener();
    }
    private void initWeb(){
       tbsContent.setVisibility(View.GONE);
        mWebView.setVisibility(View.VISIBLE);
        android.webkit.WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        mWebView.setWebChromeClient(new android.webkit.WebChromeClient());
        mWebView.setWebViewClient(new android.webkit.WebViewClient(){
            @Override
            public void onPageStarted(android.webkit.WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(android.webkit.WebView view, String url) {
                super.onPageFinished(view, url);
            }

            @Override
            public boolean shouldOverrideUrlLoading(android.webkit.WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        // 以下1行代码是测试代码，打包请注释掉
        //Toast.makeText(this,"使用系统webview加载",Toast.LENGTH_LONG).show();
        mWebView.loadUrl(vrUrl);
    }


    private void initX5(){
        tbsContent.setVisibility(View.VISIBLE);
        mWebView.setVisibility(View.GONE);

        // 以下1行代码是测试代码，打包请注释掉
      //  Toast.makeText(this,"使用X5加载",Toast.LENGTH_LONG).show();
        tbsContent.loadUrl(vrUrl);
    }
    /**
     * 启用硬件加速
     */
    private void initHardwareAccelerate() {
        try {
            if (Integer.parseInt(Build.VERSION.SDK) >= 11) {
                getWindow()
                        .setFlags(
                                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
            }
        } catch (Exception e) {
        }
    }

    public static void gotoH5(Context context, String url) {
        Intent intent = new Intent(context, TencentWebV.class);
        vrUrl = url;
        intent.putExtra(H5_URL, url);
        context.startActivity(intent);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && tbsContent.canGoBack()) {
            tbsContent.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);


    }

    public void setListener() {
        mBack.setOnClickListener(this);
        vrShareImg.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.set_vr_exit:
                finish();
                break;
            case R.id.set_vr_share:
                if (mPopShare == null) {
                    mPopShare = new PopShare(TencentWebV.this, mIwxapi, mIWeiboShareAPI, vrShareImg, Tag);
                    mPopShare.setmUrl(vrUrl);

                }
                //mPopShare.setText(vrUrl);
                mPopShare.setShowURI(DialogUrl);
                mPopShare.vrShow();
                break;
        }
    }
    @Override
    protected void onDestroy() {
        //释放资源
        if (tbsContent != null)
            tbsContent.clearCache(true);
            tbsContent.clearHistory();
            tbsContent.destroy();
        super.onDestroy();
    }

    public static String filepath;

    private String getSDCardPath() {
        File sdcardDir = null;
        // 判断SDCard是否存在
        boolean sdcardExist = Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
        if (sdcardExist) {
            sdcardDir = Environment.getExternalStorageDirectory();
        }
        return sdcardDir.toString();
    }

    private Bitmap bmp;

/*    private void setBitmap() {

        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        int w = outMetrics.widthPixels;
        int h = outMetrics.heightPixels;
        //2.获取屏幕
        View decorview = this.getWindow().getDecorView();
        decorview.setDrawingCacheEnabled(true);
        View view = getWindow().getDecorView();
        view.buildDrawingCache();
        // 获取状态栏高度
        Rect rect = new Rect();
        view.getWindowVisibleDisplayFrame(rect);
        int statusBarHeights = rect.top;
        // 允许当前窗口保存缓存信息
        view.setDrawingCacheEnabled(true);
        // 去掉状态栏
        Bitmap bitCache = view.getDrawingCache();
        if (bitCache.getHeight() >= h) {
            bmp = Bitmap.createBitmap(bitCache, 0, 0, w, h);
        }
        String SavePath = getSDCardPath() + "/jajahome/screenimage";
        File path = new File(SavePath);
        // 文件
        filepath = SavePath + "/screen_1.png";
        File file = new File(filepath);
        try {
            if (!path.exists()) {
                path.mkdirs();
            }
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(file);
            if (null != fos) {
                bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
                fos.flush();
                fos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        bitCache.recycle();
        // 销毁缓存信息
        view.destroyDrawingCache();

    }*/

}
